package com.sky.controller.admin;
import com.sky.constant.JwtClaimsConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.net.ssl.HttpsURLConnection;
import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api( tags = "员工登入接口")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;
    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("员工登入")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @ApiOperation(value = "员工退出")
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     *  添加新员工
     * @param employeeDTO
     * @return
     */
    @PostMapping()
    @ApiOperation("新增员工")
    public Result <EmployeeDTO> save( @RequestBody EmployeeDTO employeeDTO) {
        employeeService.save(employeeDTO);
        return  Result.success();
    }

    /**
     * 分页查询员工
     */
    @ApiOperation("分页查询员工")
    @GetMapping("/page")
    public  Result   pageSelectEmp (@RequestParam (defaultValue ="1")  String page ,
                                   @RequestParam  (defaultValue = "10") String  pageSize,
                                       String name )  {
      PageResult pageResult  =employeeService.pageSelectEmp(page,pageSize,name);
       Result <PageResult>result= new Result<> ();
       log.info("pageResult:{}",pageResult);
        Result<PageResult> success = result.success(pageResult);
        return   success;
    }

    /**
     * 修改员工密码
     * @param passwordEditDTO`
     * @return
     */
    //TODO 该处修改密码还是代完善状态
    @ApiOperation("修改员工密码")
    @PutMapping("/editPassword")
    public Result  updateEmpPassword( @RequestBody PasswordEditDTO passwordEditDTO) {
//        通过localThread  修改 empid
        passwordEditDTO.setEmpId(BaseContext.getCurrentId());
        log.info("passwordEditDTO:{}" ,passwordEditDTO);
          employeeService.updateEmpPassword (passwordEditDTO);
        Result<Employee> result = new Result<>();
        return  result.success();
    }

    /**
     * 根据id 查询员工
     */
    @ApiOperation("根据id  查询员工")
    @GetMapping("/{id}")
     public Result   selectEmpById ( @PathVariable String  id ) {
        log.info("查询id:{} 员工" ,id);
       Employee  employee =  employeeService.selectEmpById(id);
        Result<Employee> result = new Result<>();
           return  result.success(employee);
    }

    /**
     * 修改员工信息
     */
    @ApiOperation("修改员工属性")
    @PutMapping()
    public  Result  updateEmp(@RequestBody EmployeeDTO  employeeDTO) {
        log.info("修改的属性值：{}" ,employeeDTO);
        employeeService.updateEmp(employeeDTO);
        Result<Employee> result = new Result<>();
         return result.success();
    }

//    //   启动 /禁用 用户
    @ApiOperation("启动/禁止 用户")
    @PostMapping("/status/{status}")
    public Result  startAndDisable(@PathVariable Integer status,
                                   @RequestParam   Long id
                                    ){
        log.info("status:{},id:{},employee:{}" ,status,id);
        employeeService.updateEmpStatus(status,id);
        Result<Employee> result = new Result<>();
        return  result.success();

    }




}
